package com.orange.vod.service;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.orange.servicebase.exception.MyException;
import com.orange.vod.utils.ConstantVodUtil;
import com.orange.vod.utils.InitObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @author orange
 * @email orangestart@yeah.net
 * @create 2021-12-20 21:48
 */
@Service
public class VodServiceImpl implements VodService {

    @Override
    public String uploadVideoAliyun(MultipartFile file) {
        try {
            //accessKeyId,accessKeySecret
            //fileName：上传文件原始名称
            String fileName = file.getOriginalFilename();
            //title：上传之后显示名称
            String title = null;
            if (fileName != null) {
                title = fileName.substring(0, fileName.lastIndexOf("."));
            }
            //inputStream：上传文件的输入流
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtil.ACCESSKEY_ID
                    , ConstantVodUtil.ACCESSKEY_SECRET
                    , title, fileName
                    , inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            //请求视频点播服务的请求ID
            //System.out.print("RequestId=" + response.getRequestId() + "\n");
            return response.getVideoId();
        } catch (Exception e) {
            throw new MyException(20001, "阿里云视频点播服务上传失败");
        }
    }

    //删除视频
    @Override
    public void removeVideo(String videoId) {
        try {
            //初始化
            DefaultAcsClient client = InitObject.initVodClient(
                    ConstantVodUtil.ACCESSKEY_ID,
                    ConstantVodUtil.ACCESSKEY_SECRET);
            //创建请求对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            //获取响应
            DeleteVideoResponse response = client.getAcsResponse(request);
            //System.out.print("RequestId = " + response.getRequestId() + "\n");
        } catch (Exception e) {
            throw new MyException(20001, "视频删除失败");
        }
    }

    //删除多个视频
    @Override
    public void removeVideoList(List<String> videoIdList) {
        try {
            //初始化
            DefaultAcsClient client = InitObject.initVodClient(
                    ConstantVodUtil.ACCESSKEY_ID,
                    ConstantVodUtil.ACCESSKEY_SECRET);
            //创建请求对象
            String str = StringUtils.join(videoIdList.toArray(), ",");
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(str);
            //获取响应
            DeleteVideoResponse response = client.getAcsResponse(request);
        } catch (Exception e) {
            throw new MyException(20001, "删除多个视频失败");
        }
    }

    //根据视频id获取视频凭证
    @Override
    public String getPlayAuth(String id) {
        String accesskeyId = ConstantVodUtil.ACCESSKEY_ID;
        String accesskeySecret = ConstantVodUtil.ACCESSKEY_SECRET;

        try {
            //创建初始化对象
            DefaultAcsClient cl = InitObject.initVodClient(accesskeyId, accesskeySecret);
            //创建获取视频地址request对象和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request对象设置视频id值
            request.setVideoId(id);

            GetVideoPlayAuthResponse response = cl.getAcsResponse(request);

            //获取视频播放凭证
            return response.getPlayAuth();

        } catch (ClientException e) {
            e.printStackTrace();
            throw new MyException(20001, "获取视频凭证失败");
        }
    }
}
